4. Creating Java Projects



This chapter explains how to create and manage any Java project, and how to set the options in the Java Project settings panels.

This chapter contains these sections:


Types of Java Projects

This section discusses the various types of code objects that you can create for Java. They include the following:


Applets

An Applet is a small Java application that runs over the web, usually through a web browser. Applets can consist of multiple class files or a single Jar file.


Applications

Java Applications are executables that usually run through a Java Interpreter. Java Applications can consist of multiple class files or a single Jar file.


Libraries

A Java Library is a collection of Java classes that can be used in many projects. Libraries are usually compiled into zip files (like classes.zip) or Jar files.


Using Project Stationery

The New Project dialog box (Figure 4.1) displays a list of several different pre-configured projects and their associated files, called Project Stationery.

Figure 4.1 New Project dialog box


Each set of project stationery is pre-configured for different types of Java projects. This saves you time when creating your own project files as you only need to change a few options instead of setting up a project from scratch.

Table Table 4.1 lists each project stationery option.

Table 4.1 Project stationery options

Stationery
Description
Java Applet
Pre-configured settings for a Java applet. Output is a class folder.
Java Application
Pre-configured settings for a Java application. Output is a Jar file.
Java App-Applet
A multi-target project (Applet & Application) that demonstrates how to use AppletFrame.java to turn your applet into an application.
Java Library
Pre-configured settings for a Java library. Output is a Jar file.


NOTE:

Project stationery is configurable by the user. The supplied stationery may vary between releases of CodeWarrior. Because of this, what you see in the New Project window may be slightly different from the pictures in this manual.

Working with Java in CodeWarrior

This section describes how to create, run, and debug any kind of Java project, including applets, applications, and libraries. The topics in this section are:


Creating a New Java Project

The following procedure describes how to create a new Java project. The project can be either an applet, an application, or a library of Java code.

1. Create a new project.

Choose File > New Project. CodeWarrior displays the New Project dialog box.

2. Select project stationery.

In the New Project dialog box, select the proper stationery for the kind of project you want to create: applet, application, or library. To learn more about various kinds of application projects, see "Kinds of Application Projects."

You may also decide whether to have CodeWarrior create a new folder for you in which to place all your files. Typically you want to create a new folder.

When you click the OK button, CodeWarrior displays a file dialog.

3. Name the project.

In the file naming dialog, enter a name for the project, and click OK.

If you use a suffix on the Windows hosted CodeWarrior, it must be .mcp. If you do not use a suffix, CodeWarrior adds it for you.


TIP:

If you want your project to be recognized by all versions of CodeWarrior, the project suffix must be .mcp.

If the Create Folder check box was enabled in the New Project dialog box, CodeWarrior creates a new folder. Inside that folder, CodeWarrior puts a new project file (with all project settings identical to the stationery project you selected), and duplicates any files included in that stationery project.

For example, for an applet, you get a new project file, an HTML file, named TrivialApplet.html, and a Java source file named TrivialApplet.java.

The project file has the name you entered in the dialog box. The new folder has the same name as the project file without any naming extension.


WARNING!

The project manager also creates a directory named MyProject Data, where MyProject is the name of your project. CodeWarrior uses this directory to store internal data. Do not modify or move any file in this directory.

After creating the files and folder, CodeWarrior displays a new project window, like the one in Figure 4.1.

Figure 4.2 The Java Applet project


The stationery projects hold example files. In typical use you modify these files and save them with a new name, or replace them with your own files.

The classes.zip file is a shared library of standard Java classes your applet can use. For more information, see "Using the classes.zip Library."


Creating Java Code

To create Java code, use the CodeWarrior tools: the editor, the browser, and the project manager. For details on these tools, see the IDE User Guide.

In typical practice, you start with a project based on stationery, which includes example or "placeholder" files. To learn how to create a project in this way, see "Creating a New Java Project."

For example, if you are creating an applet based on the Java applet stationery, there are two such files: TrivialApplet.java and TrivialApplet.html.

The TrivialApplet.java file contains a sample Java applet. You can open the file, replace its contents with your own code, and save the file under a new name. You can add any other source file as well.


NOTE:

Each Java file must contain only one public class. The file name must be the same name as the public class with the suffix .java. For example, if the name of your class is MyClass, the Java file must be named MyClass.java. If you do not follow this convention, CodeWarrior may compile your project anyway, but other Java compilers may not.

Similarly, the TrivialApplet.html file contains sample HTML code. You can modify this file so that it displays your applet. You typically specify both the location of the applet (a folder name) and the name of the class containing the main function. You can set the name of your classes folder by opening the Target Settings dialog box, and modifying the name specified in the Output panel. See "Target Settings" and "Java Output" for more information.


Changing Settings

If you use project stationery as a starting point, you generally will not need to change many of the settings. The default settings are perfect for most projects. However, this is not the case for all settings or all projects. This is especially true if you have more than one target in a project. You can modify a variety of settings for Java projects. Each panel is explained in detail in "Target Settings for Java."

Common settings you might change are the main class (for Java applications) and the output name (the name of your final program). For applications you may also choose whether to output your files in a class folder, or in a zip file.


Specifying the main class

To tell CodeWarrior the name of your main class for a Java application, go to the Java Target panel, make sure that you are creating an application, and set the name in the Main Class field in the panel. For more information, see "Main Class."


Specifying output name

To tell CodeWarrior what to name your final output file (be it an applet or an application), go to the Java Output panel and enter the name in the Name field. For more information, see "Java Output."


Specifying output type

To tell CodeWarrior whether to create a class folder or a Jar file, go to the Java Target panel and set the Output Type option. For more information, see "Java Target."


Running a Java Project

You may run applets and applications. You cannot run libraries. To run a project, choose Project > Run. At that time, CodeWarrior compiles and links any changed files, and launches your program. Precisely how your program launches depends upon whether it is an applet or an application.


Running a Java applet

When developing an applet, you typically have a small HTML file that embeds the applet code. CodeWarrior uses this file to run your program from within the IDE. When you choose Project > Run, CodeWarrior opens the first HTML file in the Link Order view with an HTML browser or applet viewer. In other words, the order of HTML files in the File view in the Project Window does not matter. It is the order as shown in the Link Order view in the Project Window.


NOTE:

For applet projects, if there is no HTML file in your project, the Run command is disabled.

You can add as many HTML files as you want to a project. If your applet is complex, you may want several HTML files that test different aspects of your applet. To change the HTML file that CodeWarrior uses, simply rearrange the files in the Link Order view of your project window.


NOTE:

You can also run an applet by opening one of its HTML files with any Java-enabled browser.

Running a Java application

To run an application, choose Project > Run in the CodeWarrior IDE. The Virtual Machine chosen in the Java Target settings panel loads the application's code and runs the main method of the application's main class. If you entered a value in the Main Class field in the Java Target settings panel, it will use that class. Otherwise, it will assume the main class is the first class in the first file in the project window. For more information on how to specify the main class, see "Main Class."


NOTE:

On the Mac OS, if you run a droplet by dragging and dropping files onto it, the full pathnames of those files are passed as arguments to the main class's main method. Note that if you run a droplet by double-clicking it or choosing Project > Run, you cannot specify arguments for it.

Debugging a Java Project

You debug an applet project the same way you debug any other project. For more information on the CodeWarrior debugger, consult the IDE User Guide. For more information on Java-specific features in the debugger, see "Debugging Java Projects."

To debug a Java project, follow these steps:

1. Turn on debugging.

This step can be skipped if debugging is already enabled.

Choose Project > Enable Debugger. You may see a dialog box telling you that certain settings must be modified for debugging. Click the OK button, and CodeWarrior sets up debugging automatically. In the project window, black dots appear in the debugging column next to the source files.

2. Run the project under the debugger.

Choose Project > Debug. CodeWarrior compiles any changed files and generates debugging information for all files that have a black dot in the debugging column of the project window. CodeWarrior launches your project using the appropriate mechanism for your project. For a discussion of this process, see "Running a Java Project."

Once the project is running, control returns to the debugger. You can now debug the applet like you would any other program. For more information on the debugger, see the Debugger User Guide. For more information on Java-specific features in the debugger, see "Debugging Java Projects."


Kinds of Application Projects

CodeWarrior lets you create Java applications that you can run without any applet viewer or browser.

These are the other types of applications you might create for specific purposes:

All these files types contain Java bytecodes that will run on any computer platform that supports Java. However, standalone application files use special utilities to launch them without the help of java-enabled browsers.

Use an application when you will use the file only on a Mac OS computer. Use a jar file or class folder when you will use the file on other computer platforms as well.


Using the classes.zip Library

The classes.zip file is a library of Java classes that all Java programs share. It includes all the packages in the Java API, such as the Abstract Windowing Toolkit (AWT), I/O, and Applet packages.

The location depends on where JDK is installed on your machine. Following is a list of the locations where the classes.zip file can normally be found:


Windows

inside the Java folder of the System folder


Mac OS

in a Classes folder in a subfolder of your Extensions folder


Solaris

in the lib directory of the JDK directory

Almost every Java program that you create uses classes.zip. However, classes.zip is a large file (about 1.5 megabytes). So you probably do not want to copy it into every one of your Java applications and applets.

Figure 4.3 The Import Weak Classes Setting


To instruct CodeWarrior not to merge the Classes.zip file into the final output, first select the Classes.zip file in the project window. Then choose Window > Project Inspector to open the Project Inspector. Make sure that the Merge Into Output checkbox is not checked. CodeWarrior do not copy the library into your finished program.

To enable your project to compile without the Classes.zip library, make sure that the Import Weak checkbox is checked. With these settings, your program will automatically look for the library on the host system when it is executed.

The Project Inspector window should now appear as in Figure 4.3.

classes.zip is automatically set up this way for you whenever you create a project with Metrowerks-supplied project stationery,


NOTE:

The Initialize Before option is ignored by the CodeWarrior Java compiler. Merge Into Output will add Classes.zip to the final binary.





Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: support@metrowerks.com
Copyright © 1999, Metrowerks Corp. All rights reserved.

Last updated: May 24, 1999 * Chris Magnuson * John Roseborough